package com.github.common.extend.demo.util.iterator;

import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.Queue;

/**
 * @author wangjj7
 * @date 2023/9/26
 * @description
 */
public class XProducerIterator<E> implements Iterator<E> {

    private final Queue topicQueue = new ArrayDeque();

    private final Iterator<E> iterator;

    public XProducerIterator(Iterator<E> iterator){
        this.iterator = iterator;
    }

    @Override
    public boolean hasNext() {
        return iterator.hasNext();
    }

    @Override
    public E next() {
        E next = iterator.next();
        this.topicQueue.add(next);
        return next;
    }

    public Iterator<E> consumer(){
        return new IteratorConsumer<>(topicQueue);
    }

    private class IteratorConsumer<E> implements Iterator<E> {

        private final Queue<E> queue;

        public IteratorConsumer(Queue<E> queue){
            this.queue = queue;
        }

        @Override
        public boolean hasNext() {
            return !queue.isEmpty();
        }

        @Override
        public E next() {
            return queue.poll();
        }

    }

}
